<?php
namespace app\backstage\logic;
use think\Db;
class Backhaul
{
    /*列表数据获取*/
    public function getList($params)
    {
        $field = 'id,channel_id,advert_media_id,report_ab,report_jb,report_rb,report_ao,misreporting_loop_proportion,misreporting_type,';
        $field .= 'report_old_fans,report_caliber,report_full,report_order_bn,misreporting_percentage,status';

        if (request()->_role->alias == 'agent') {
            $channel_ids = Db::table('cms_manager_user')->where('parent_id', '=', request()->_user->id)
                ->column('id');
            $params['channel_id'] = $channel_ids ? $channel_ids : [0];
        }
        if (request()->_role->alias == 'channel') {
            $params['channel_id'] = request()->_user->id;
        }

        $condition[] = ['channel_id', 'in', 'not_empty', 'channel_id'];
        $condition[] = ['advert_media_id', 'eq', 'gt:0', 'advert_media_id'];
        $condition[] = ['id', 'eq', 'require', 'id'];

        $where = buildQueryCondition($condition, $params);

        if (isset($params['field'])) {
            $orderBy = [$params['field'] => $params['order']];
        } else {
            $orderBy = ['id' => 'DESC'];
        }

        $Backhaul = \think\facade\App::model('Backhaul');
        $list = $Backhaul->getList($where, $field, $params['page'], $params['limit'], $orderBy);

        if ($list) {

            $channel_id_gh = array_unique(array_column($list, 'channel_id'));
            $channels = Db::table('cms_manager_user')->where('id', 'in', $channel_id_gh)->column('id,account');

            $state    = ['2'=>'<span style="color:red">否</span>','1'=>'<span style="color:green">是</span>'];
            $state_ab    = ['3'=>'<span style="color:red">否</span>','1'=>'<span style="color:green">上报加桌行为</span>','2'=>'<span style="color:green">上报注册行为</span>'];

            foreach ($list as $k => $v) {
                $list[$k]['channel_name']           = isset($channels[$v['channel_id']]) ? $channels[$v['channel_id']] : '';
                $list[$k]['media_name']             = getAdvertMediaName($v['advert_media_id']);
                $list[$k]['report_caliber']         = getReportCaliberTitle($v['report_caliber']);
                $list[$k]['report_ao']              = $state[$v['report_ao']];
                $list[$k]['report_ab']              = $state_ab[$v['report_ab']];
                $list[$k]['state']                  = $state[$v['status']];
                $list[$k]['misreporting_percentage'] = $v['misreporting_percentage'].'%';

            }
        }

        $result['count'] = $Backhaul->getTotalNumber($where);
        $result['list'] = $list;
        return $result;
    }

    /*数据获取*/
    public function getInfo($where)
    {
        $field = 'id,channel_id,advert_media_id,report_ab,report_jb,report_rb,report_ao,misreporting_loop_proportion,misreporting_type,';
        $field .= 'report_old_fans,report_caliber,report_full,report_order_bn,misreporting_percentage,status,order_amount_below,order_amount_above,hourValue,vip_order_switch';
        $Backhaul = \think\facade\App::model('Backhaul');

        $info = $Backhaul->getOne($where,$field);

        return $info;
    }

    /*数据添加*/
    public function addInfo($data)
    {
        $Backhaul = \think\facade\App::model('Backhaul');
        $res = $Backhaul->insertRecord($data);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*数据编辑*/
    public function editInfo($where,$data)
    {
        $Backhaul = \think\facade\App::model('Backhaul');
        $res = $Backhaul->updateRecord($where,$data);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*数据删除*/
    public function delInfo($where)
    {
        $Backhaul = \think\facade\App::model('Backhaul');
        $res = $Backhaul->deleteRecord($where);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*数据处理*/
    public function dataProcessing($params)
    {
        if (request()->_role->alias == 'channel') {
            $data['channel_id'] = request()->_user->id;
        }else{
            $data['channel_id']             = $params['channel_id'];
        }

        $data['advert_media_id']        = $params['advert_media_id'];
        $data['report_ab']              = $params['report_ab'];
        $data['report_ao']              = $params['report_ao'];
        $data['report_caliber']         = $params['report_caliber'];
        $data['report_full']            = $params['report_full'];
        $data['report_order_bn']        = $params['report_order_bn'];
        $data['status']                 = $params['status'];

        if(isset($params['misreporting_loop_proportion']))
        {
            $data['misreporting_loop_proportion'] = $params['misreporting_loop_proportion'];
        }
        if(isset($params['misreporting_percentage']))
        {
            $data['misreporting_percentage'] = $params['misreporting_percentage'];
        }
        if(isset($params['misreporting_type']))
        {
            $data['misreporting_type'] = $params['misreporting_type'];
        }
        if(isset($params['order_amount_below']))
        {
            $data['order_amount_below'] = $params['order_amount_below'];
        }
        if(isset($params['order_amount_above']))
        {
            $data['order_amount_above'] = $params['order_amount_above'];
        }
        if(isset($params['hourValue']))
        {
            $data['hourValue'] = $params['hourValue'];
        }
        if(isset($params['vip_order_switch']))
        {
            $data['vip_order_switch'] = $params['vip_order_switch'];
        }

        return ['data'=>$data];
    }

}